#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.61)
AC_INIT([urbackup-client], [2.5.11.BUILDID], [martin@urbackup.org])
AC_CONFIG_SRCDIR([AcceptThread.cpp])
AC_CONFIG_HEADER([config.h])
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE([subdir-objects])

AC_ARG_ENABLE([headless],
     AS_HELP_STRING([--enable-headless], [Do not compile and install the GUI components]))
	 
AC_ARG_ENABLE([install_debian_initd],
     AS_HELP_STRING([--enable-install-debian-initd], [Enables installing of supplied Debian init.d file into /etc/init.d]))	 
AM_CONDITIONAL(INSTALL_DEBIAN_INITD, test "x$enable_install_debian_initd" = xyes)

AC_ARG_ENABLE([install_redhat_initd],
     AS_HELP_STRING([--enable-install-redhat-initd], [Enables installing of supplied RedHat init.d file into /etc/init.d]))	 
AM_CONDITIONAL(INSTALL_REDHAT_INITD, test "x$enable_install_redhat_initd" = xyes)

AC_ARG_ENABLE([fortify],
     AS_HELP_STRING([--disable-fortify], [Disable GCC/runtime fortification]))
AM_CONDITIONAL(WITH_FORTIFY, test "x$enable_fortify" != xno)

AC_ARG_ENABLE([clientupdate],
     AS_HELP_STRING([--enable-clientupdate], [Enables the internal client update functionality]))
AM_CONDITIONAL(CLIENT_UPDATE, test "x$enable_clientupdate" = xyes)

AC_ARG_ENABLE([embedded-cryptopp],
     AS_HELP_STRING([--enable-embedded-cryptopp], [Compile and use Crypto++ 5.6.3 included with the source distribution.]))
AM_CONDITIONAL(EMBEDDED_CRYPTOPP, test "x$enable_embedded_cryptopp" = xyes)
AM_CONDITIONAL(TARGET_CPU_IS_X86, test "x$TARGET_CPU" = xx86)

AC_ARG_ENABLE([assertions],
     AS_HELP_STRING([--enable-assertions], [Enable assertions (bug finding).]))
AM_CONDITIONAL(WITH_ASSERTIONS, test "x$enable_assertions" = xyes)

AC_ARG_WITH([embedded-sqlite3],
     AS_HELP_STRING([--without-embedded-sqlite3], [Disables the embedded sqlite3 and uses the system one. Not recommended.]))
AM_CONDITIONAL(WITH_EMBEDDED_SQLITE3, test "x$with_embedded_sqlite3" != "xno")

AC_ARG_ENABLE([c-ares],
     AS_HELP_STRING([--enable-c-ares], [Use c-ares for DNS resolution instead of your system libc.]))
AM_CONDITIONAL(ENABLE_C_ARES, test "x$enable_c_ares" = xyes)

AC_ARG_ENABLE([embedded-zstd],
     AS_HELP_STRING([--enable-embedded-zstd], [Compile and use ZSTD included with the source distribution.]))
AM_CONDITIONAL(EMBEDDED_ZSTD, test "x$enable_embedded_zstd" = xyes)

AC_ARG_WITH([embedded-aws-cpp-sdk],
     AS_HELP_STRING([--without-embedded-aws-cpp-sdk], [Disables the embedded AWS c++ sdk (for S3) and uses the system one. Not recommended.]))
AM_CONDITIONAL(WITH_EMBEDDED_AWS_CPP_SDK, test "x$with_embedded_aws_cpp_sdk" != "xno")

AM_CONDITIONAL(WITH_BTRFS_ZLIB, test 1 = 0)

AC_ARG_ENABLE([httpserver],
     AS_HELP_STRING([--enable-httpserver], [Enable HTTP server for restore CD]))
AM_CONDITIONAL(WITH_HTTPSERVER, test "x$enable_httpserver" = xyes)

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_OBJCXX
AC_PROG_EGREP
AC_PROG_GREP

AX_PTHREAD
if !($HAVE_PTHREAD)
then
	echo "Sorry, your system needs the pthread library."
	echo "Either install it or give up."
	exit 1
fi

AC_LANG([C++])

AX_CXX_COMPILE_STDCXX([17])

# Checks for libraries.

AX_CHECK_OPENSSL([with_openssl=true], [with_openssl=false])
AM_CONDITIONAL([WITH_OPENSSL], [test x$with_openssl = xtrue])

if test "x$with_openssl" != "xtrue"
then
    AC_MSG_ERROR([OpenSSL library not present or not recent enough.])
fi

LIBCURL_CHECK_CONFIG(,[7.20.0],,[
       AC_MSG_ERROR([Your system lacks of libcurl >= 7.20.0])
     ])

if test "x$with_embedded_aws_cpp_sdk" = "xno"
then
	AC_CHECK_HEADER(aws/s3/S3Client.h, [HAS_AWS_SDK_H=1])
	AC_CHECK_LIB(aws-c-common, aws_string_destroy, [HAS_AWS_C_COMMON=1])
	AC_MSG_CHECKING([linking to aws-c++-sdk])
	SAVED_LDFLAGS="$LDFLAGS"
	SAVED_LIBS="$LIBS"
	SAVED_CPPFLAGS="$CPPFLAGS"
	AWS_SDK_INCLUDES=""
	AWS_SDK_LIBS="-laws-cpp-sdk-s3 -laws-cpp-sdk-core -laws-c-event-stream -laws-checksums -laws-c-common"
	LDFLAGS="$LDFLAGS"
	CPPFLAGS="$CPPFLAGS $PTHREAD_CFLAGS $AWS_SDK_INCLUDES"
	LIBS="$LIBS $AWS_SDK_LIBS $OPENSSL_LIBS $LIBS $LIBCURL $PTHREAD_LIBS"
	AC_LINK_IFELSE(
		[AC_LANG_PROGRAM([#include <aws/s3/model/BucketLocationConstraint.h>], 
			[Aws::S3::Model::BucketLocationConstraintMapper::GetNameForBucketLocationConstraint(Aws::S3::Model::BucketLocationConstraint::af_south_1);])],
		[
			HAS_AWS_CPP_SDK=1
			AC_MSG_RESULT([yes])
		], 
		[AC_MSG_RESULT([no])])
	LDFLAGS="$SAVED_LDFLAGS"
	CPPFLAGS="$SAVED_CPPFLAGS"
	LIBS="$SAVED_LIBS"
	AC_SUBST([AWS_SDK_INCLUDES])
	AC_SUBST([AWS_SDK_LIBS])

	if test "x$HAS_AWS_SDK_H" != "x1" || test "x$HAS_AWS_C_COMMON" != "x1" || test "x$HAS_AWS_CPP_SDK" != "x1"
	then
		AC_MSG_ERROR(AWS C++ SDK not found or installed correctly)
	fi
else
	LIBCURL_CHECK_CONFIG(,[7.20.0],,[
       AC_MSG_ERROR([Your system lacks of libcurl >= 7.20.0])
     ])
fi

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([pthread.h arpa/inet.h fcntl.h netdb.h netinet/in.h stdlib.h sys/socket.h sys/time.h unistd.h mntent.h spawn.h linux/fiemap.h sys/random.h linux/fs.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

AC_CXX_HAVE_SSTREAM
AC_CXX_HAVE_STRSTREAM
AC_CXX_HAVE_LONG_LONG

AX_CHECK_COMPILE_FLAG(-fstack-protector-strong --param=ssp-buffer-size=4 -Wformat -Werror=format-security -fPIE,
	[], [AM_CONDITIONAL(WITH_FORTIFY, test xyes = xno)])

AX_CHECK_COMPILE_FLAG(-msse4.1 -msse4.2, 
	[AM_CONDITIONAL(WITH_SSE4_2, test xyes = xyes)],
	[AM_CONDITIONAL(WITH_SSE4_2, test xyes = xno)])

AX_CHECK_COMPILE_FLAG(-Wno-subobject-linkage,
	[AM_CONDITIONAL(DISABLE_CERTAIN_WARNINGS, test xyes = xyes)], 
	[AM_CONDITIONAL(DISABLE_CERTAIN_WARNINGS, test xyes = xno)])
	
AX_CHECK_LINK_FLAG([-Wl,-z,relro],
	[], [AM_CONDITIONAL(WITH_FORTIFY, test xyes = xno)])

AC_MSG_CHECKING([for operating system])
case "$host_os" in
freebsd*)
        opsys="freebsd"
        ;;
linux*)
        opsys="linux"
        ;;
darwin*)
		opsys="osx"
        ;;
*)
        opsys=""
        ;;
esac
AM_CONDITIONAL([FREEBSD], test x"$opsys" = x"freebsd")
AM_CONDITIONAL([LINUX], test x"$opsys" = x"linux")
AM_CONDITIONAL([MACOSX], test x"$opsys" = x"osx")

AX_LIB_SOCKET_NSL
AX_CHECK_ZLIB

# Checks for library functions.
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_STRFTIME
AC_CHECK_FUNCS([gettimeofday memset select socket strstr syncfs fallocate64 utimensat accept4 getrandom])
AC_CHECK_LIB(dl, dlopen, [DLOPEN_LIBS="-ldl"])
AC_SUBST([DLOPEN_LIBS])

if test "x$enable_embedded_zstd" != "xyes"
then
	AC_CHECK_LIB(zstd, ZSTD_CCtx_setParameter, [HAS_ZSTD_LIB=1])
	AC_CHECK_HEADER(zstd.h, [HAS_ZSTD_HEADER=1])
	if test "x$HAS_ZSTD_LIB" != "x1" || test "x$HAS_ZSTD_HEADER" != "x1"
	then
        AC_MSG_WARN([ZSTD compression library not present or not recent enough. Compiling without ZSTD support. Install libzstd-dev(el) or use --enable-embedded-zstd])
	fi
	AM_CONDITIONAL(WITH_ZSTD, test "x$HAS_ZSTD_LIB" == "x1" && test "x$HAS_ZSTD_HEADER" == "x1")
else
	AM_CONDITIONAL(WITH_ZSTD, true)
fi

if test "x$enable_embedded_cryptopp" != "xyes"
then
	MULE_CHECK_CRYPTOPP([5.1], [], [echo "Crypto++ not found. Please install (cryptopp-devel/libcrypto++-dev) or run configure with --enable-embedded-cryptopp."; exit 44;])
	
	SAVED_LDFLAGS="$LDFLAGS"
	SAVED_CPPFLAGS="$CPPFLAGS"
	SAVED_LIBS="$LIBS"
	LDFLAGS="$LDFLAGS $CRYPTOPP_LDFLAGS"
	CPPFLAGS="$CPPFLAGS $CRYPTOPP_CPPFLAGS $PTHREAD_CFLAGS"
	LIBS="$LIBS $CRYPTOPP_LIBS $PTHREAD_LIBS"
	CRYPTOPP_INC="<$CRYPTOPP_INCLUDE_PREFIX/misc.h>"
	AC_MSG_CHECKING([whether Crypto++ contains CryptoPP::AlignedAllocate function])
	AC_LINK_IFELSE(
	  [AC_LANG_PROGRAM([#include $CRYPTOPP_INC], 
		[CryptoPP::AlignedAllocate(5);])],
	  [AC_MSG_RESULT([yes])], 
	  [
		CRYPTOPP_CPPFLAGS="$CRYPTOPP_CPPFLAGS -DCRYPTOPP_DISABLE_ASM"
		AC_MSG_RESULT([no])
	  ])
	LDFLAGS="$SAVED_LDFLAGS"
	CPPFLAGS="$SAVED_CPPFLAGS"
	LIBS="$SAVED_LIBS"
else
	AC_CONFIG_SUBDIRS([cryptoplugin/src])
fi

if test "x$enable_c_ares" = "xyes"
then
	AC_CHECK_LIB(cares, ares_getaddrinfo, [HAVE_C_ARES=1])
	AS_IF([test "x$HAVE_C_ARES" = "x"],
	[
		AC_MSG_ERROR([
				C-Ares (address resolution) library not found or not recent enough.
				])
	])
fi

AM_CONDITIONAL(WITH_GUI_CLIENT, test "x$enable_headless" != "xyes")

AS_IF([test "x$enable_headless" != "xyes"],
[
	AM_OPTIONS_WXCONFIG
	reqwx=2.9.0
	AM_PATH_WXCONFIG($reqwx, wxWin=1)
	if test "$wxWin" != 1; then
		AC_MSG_ERROR([
			wxWidgets must be installed on your system.
	 
			Please check that wx-config is in path, the directory
			where wxWidgets libraries are installed (returned by
			'wx-config --libs' or 'wx-config --static --libs' command)
			is in LD_LIBRARY_PATH or equivalent variable and
			wxWidgets version is $reqwx or above.
			
			Alternatively you can run ./configure with the
			--enable-headless switch to not build and install
			the GUI component (tray icon).
			])
	fi
])

if test "x$with_embedded_sqlite3" == "xno"
then
	AX_LIB_SQLITE3([3.8.7.0])
	
	AS_IF([test "x$HAVE_SQLITE3" = "x"],
	[
		AC_MSG_ERROR([
				Specified to not use embedded sqlite3, but no system sqlite3 found.
				
				Need sqlite3 to compile.
				])
	])
fi

AS_IF([test "x$opsys" != "xosx"],
[
	AC_SEARCH_LIBS([clock_gettime], [rt posix4],
		[test "$ac_cv_search_clock_gettime" = "none required" || LIBS="$LIBS $ac_cv_search_clock_gettime"],
		[AC_MSG_FAILURE([No library for clock_gettime found])] )
])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
